home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / grvecs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  13.3 KB  |  414 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  grvecs.c
  5.  
  6.    Copyright (C) 1992, 1993, 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:ADS version of the AutLISP program sprial.lsp
  29.  
  30.    Author     :
  31.                  Autodesk, Inc.
  32.                  2320 Marinship Way
  33.                  Sausalito, CA. 94965
  34.                  (415)332-2344
  35.  
  36.   **************************************************************************
  37.  
  38.     Function Entry Points:
  39.         void
  40.         main(argc, argv)
  41.         int    argc;              [Number of argument passed in cmd line]
  42.         char   *argv[];           [Array of pointers to arguments]
  43.  
  44.     Exported ADS Functions
  45.         TESTGRVECS                [Test the ADS_GRVECS function]
  46.  
  47.     Modification History:
  48.         Feb 28 1992 - bcm - original creation
  49.  
  50.     Notes and restrictions on use:
  51.  
  52.  
  53. ***************************************************************************/
  54.  
  55. /**************************************************************************/
  56. /*  MODULE NAME  */
  57. /**************************************************************************/
  58. #define    GRVECS
  59.  
  60. /****************************************************************************/
  61. /*  DEFINES  */
  62. /****************************************************************************/
  63. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  64. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  65. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  66. /**************************************************************************/
  67. /*  TYPEDEFS  */
  68. /**************************************************************************/
  69. /* ADS Function Table */
  70. typedef struct {
  71.     char    *name;
  72.     int     (*fptr)();
  73. } ftblent;
  74.  
  75. typedef struct resbuf rbtype;
  76. /**************************************************************************/
  77. /*  INCLUDES  */
  78. /**************************************************************************/
  79.  
  80. #include <stdio.h>
  81. #include <math.h>
  82. #ifndef __WATCOMC__
  83. #include <memory.h>
  84. #endif
  85. #include "adslib.h"
  86. /****************************************************************************/
  87. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  88. /****************************************************************************/
  89. int grspiral   _((int ntimes, ads_point bpoint, ads_real cfac, int lppass));
  90. int testgrvecs _((void));
  91.  
  92. /**************************************************************************/
  93. /*  GLOBAL VARIABLES  */
  94. /**************************************************************************/
  95. /* Table of ADS functions */
  96. ftblent exfun[] = {
  97.             {/*MSG0*/"C:TESTGRVECS", testgrvecs},
  98.         };
  99.  
  100. /**************************************************************************/
  101. /*  EXTERNAL FUNCTION DECLARATIONS  */
  102. /**************************************************************************/
  103.  
  104. /**************************************************************************/
  105. /*  EXTERNAL VARIABLE DECLARATIONS  */
  106. /**************************************************************************/
  107.  
  108. /****************************************************************************/
  109. /*  LOCAL FUNCTION DECLARATIONS  */
  110. /****************************************************************************/
  111. int funcload   _((void));
  112. int funcunload _((void));
  113. int dofun      _((void));
  114.  
  115. /******************************************************************************/
  116. /*.doc geterrno(internal) */
  117. /*+
  118.     This function is called to obtain the value of the AutoCAD system
  119.     variable ERRNO and return it as a result.
  120. -*/
  121. /******************************************************************************/
  122. int
  123. /*FCN*/geterrno()
  124. {
  125.     rbtype errval;
  126.  
  127.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  128.  
  129.     return errval.resval.rint;
  130. }
  131.  
  132. /******************************************************************************/
  133. /*.doc funcload(internal) */
  134. /*+
  135.     This function is called to define all function names in the ADS
  136.     function table.  Each named function will be callable from lisp or
  137.     invokable from another ADS application.
  138. -*/
  139. /******************************************************************************/
  140. int
  141. /*FCN*/funcload()
  142. {
  143.     int i;
  144.  
  145.     for (i = 0; i < ELEMENTS(exfun); i++) {
  146.         if (!ads_defun(exfun[i].name, i))
  147.             return RTERROR;
  148.     }
  149.  
  150.     return RTNORM;
  151. }
  152.  
  153. /******************************************************************************/
  154. /*.doc funclunoad(internal) */
  155. /*+
  156.     This function is called to undefine all function names in the ADS
  157.     function table.  Each named function will be removed from the
  158.     AutoLISP hash table.
  159. -*/
  160. /******************************************************************************/
  161. int
  162. /*FCN*/funcunload()
  163. {
  164.     int i;
  165.  
  166.     /* Undefine each function we defined */
  167.  
  168.     for (i = 0; i < ELEMENTS(exfun); i++) {
  169.         ads_undef(exfun[i].name,i);
  170.     }
  171.  
  172.     return RTNORM;
  173. }
  174. /******************************************************************************/
  175. /*.doc dofun(internal) */
  176. /*+
  177.     This function is called to invoke the function which has the
  178.     registerd function code that is obtained from  ads_getfuncode.  The
  179.     function will return RTERROR if the function code is invalid, or
  180.     RSERR if the invoked function fails to return RTNORM.  The value
  181.     RSRSLT will be returned if the function code is valid and the
  182.     invoked subroutine returns RTNORM.
  183. -*/
  184. /******************************************************************************/
  185. int
  186. /*FCN*/dofun()
  187. {
  188.     int    val;
  189.     int    rc;
  190.  
  191.     ads_retvoid();
  192.  
  193.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  194.         return RTERROR;
  195.  
  196.     rc = (*exfun[val].fptr)();
  197.  
  198.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  199. }
  200.  
  201. /******************************************************************************/
  202. /*.doc main(internal) */
  203. /*+
  204.     This is the main entry point for the ADS application.  All ADS
  205.     requests will be dispatched from this function.  This is your
  206.     standard ADS dispatch loop.
  207. -*/
  208. /******************************************************************************/
  209. void
  210. /*FCN*/main(argc,argv)
  211. int argc;
  212. char *argv[];
  213. {
  214.     short scode = RSRSLT;          /* Normal result code (default) */
  215.     int   stat;
  216.     char errmsg[80];
  217.  
  218.     ads_init(argc, argv);          /* Initiate communication with AutoLISP */
  219.  
  220.     for ( ;; ) {                   /* Request/Result loop */
  221.  
  222.         if ((stat = ads_link(scode)) < 0) {
  223.             sprintf(errmsg,
  224.                     /*MSG1*/"GRVECS: bad status from ads_link() = %d\n",
  225.                     stat);
  226. #ifdef Macintosh
  227.             macalert(errmsg);
  228. #else
  229.             puts(errmsg);
  230.             fflush(stdout);
  231. #endif /* Macintosh */
  232.             exit(1);
  233.         }
  234.  
  235.         scode = RSRSLT;           /* Reset result code */
  236.  
  237.         switch (stat) {
  238.  
  239.         case RQXLOAD:             /* Load & define functions */
  240.             scode = funcload() ? -RSRSLT : -RSERR;
  241.             break;
  242.  
  243.         case RQXUNLD:             /* Unload functions */
  244.             scode = funcunload() ? -RSRSLT : -RSERR;
  245.             ads_printf(/*MSG2*/"Unloading.\n");
  246.             break;
  247.  
  248.         case RQSUBR:             /* Handle request for external function */
  249.             dofun();
  250.             break;
  251.  
  252.         default:
  253.             break;
  254.         }
  255.     }
  256. }
  257.  
  258. /******************************************************************************/
  259. /*.doc grspiral(internal) */
  260. /*+
  261.     This function is a C translation of the grspiral.lsp program
  262.     designed to exersise the AutoLISP function grvecs.  The function
  263.     takes the arguments: number of spiral rotations, the center point
  264.     for the spiral, the rate of growth per rotation, and the number of
  265.     points per rotation.
  266.  
  267.     The vectors for the spiral will be generated in the form of a
  268.     linked list of three dimentional point nodes, and color short
  269.     nodes.  The resulting list will be passed to the ads_grvecs
  270.     function which will perform the actual graphical output.  Each
  271.     vector in the list will include a color value which is obtained by
  272.     cycling thru the range of colors specified by the values of minclr,
  273.     and maxclr.
  274.  
  275.     This function always returns RTNORM.
  276. -*/
  277. /******************************************************************************/
  278. int
  279. /*FCN*/grspiral(ntimes, bpoint, cfac, lppass)
  280. int       ntimes;
  281. ads_point bpoint;
  282. ads_real  cfac;
  283. int       lppass;
  284. {
  285.     ads_real     circle;
  286.     ads_real     ainc;
  287.     ads_real     dinc;
  288.     ads_real     ang;
  289.     ads_real     dist;
  290.     ads_real     zdir;
  291.     ads_point    lpoint;
  292.     ads_point    tp;
  293.     rbtype       *plist;
  294.     rbtype       *tplist;
  295.     int          minclr;
  296.     int          maxclr;
  297.     int          color;
  298.     int          n;
  299.     int          l;
  300.     int          sindex;
  301.     ads_matrix   mat;
  302.  
  303.     extern ads_matrix ads_identmat;
  304.     memcpy(mat, ads_identmat, sizeof(ads_matrix));
  305.     CPOINT(lpoint, bpoint);
  306.     circle = 3.141496235 * 2;
  307.     ainc = circle / lppass;
  308.     dinc = cfac / lppass;
  309.     ang = 0.0;
  310.     dist = 0.0;
  311.     minclr = 1;
  312.     maxclr = 200;
  313.     color = minclr;
  314.     zdir = 0.0;
  315.  
  316.     ads_retnil();
  317.     tplist = plist = ads_buildlist(RTSHORT, color, NULL);
  318.  
  319.     for (n = ntimes; n > 0 && tplist != NULL; n--) {
  320.         for (l = lppass; l > 0 && tplist != NULL; l--) {
  321.             ang = ang + ainc;
  322.             dist = dist + dinc;
  323.             ads_polar(bpoint, ang, dist, tp);
  324.  
  325.             /* adjust z coordinate to this point */
  326.             tp[Z] = zdir;
  327.  
  328.             /* append last point and new point to end of grvecs list */
  329.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, lpoint, NULL);
  330.             if (tplist == NULL)
  331.                 continue;
  332.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, tp, NULL);
  333.             if (tplist == NULL)
  334.                 continue;
  335.  
  336.             /* increment color */
  337.             color++;
  338.  
  339.             /* if color is == max color then reset it to one and
  340.                loop thru them again */
  341.             if (color == maxclr)
  342.                 color = minclr;
  343.  
  344.             /* append new color number to end of grvecs list */
  345.             tplist = tplist->rbnext = ads_buildlist(RTSHORT, color, NULL);
  346.             if (tplist == NULL)
  347.                 continue;
  348.  
  349.             /* save new point as last point */
  350.             CPOINT(lpoint, tp);
  351.  
  352.             /* adjust the z coord for the next point */
  353.             zdir = zdir + cfac;
  354.         }
  355.     }
  356.     if (plist != NULL && tplist != NULL)
  357.         ads_grvecs(plist, NULL);
  358.         for (sindex = 0; sindex < 100; sindex ++) {
  359.             mat[X][X] = mat[Y][Y] = mat[Z][Z] += .05;
  360.             ads_grvecs(plist, mat);
  361.         }
  362.     if (plist != NULL)
  363.         ads_relrb(plist);
  364.     return RTNORM;
  365. }
  366.  
  367.  
  368. /******************************************************************************/
  369. /*.doc testgrvecs(internal) */
  370. /*+
  371.     This function is called from dofun function as a result of RQSUBR
  372.     request being sent to the main dispatch loop.
  373.  
  374.     It prompts the user for the Center point of a spiral, the number of
  375.     rotations in the spiral, the amount of growth per rotation, and the
  376.     number of points in each rotation.   The function can be canceled
  377.     by the user by pressing the Control-C keys.
  378.  
  379.     This function does not perfrom an error checking.  The return value
  380.     for this function is the last value in rc.
  381. -*/
  382. /******************************************************************************/
  383. int
  384. /*FCN*/testgrvecs()
  385. {
  386.     rbtype      *args;
  387.     int         rc;
  388.     ads_point   bp;
  389.     int         nt;
  390.     ads_real    cf;
  391.     int         lp;
  392.  
  393.     args = ads_getargs();
  394.     ads_printf(/*MSG3*/"\nPerforming ads_grvecs test\n");
  395.     if (args != NULL) {
  396.         ads_printf(/*MSG4*/"No arguments please.\n");
  397.     }
  398.     rc = ads_getpoint(NULL, /*MSG5*/"\nCenter point: ", bp);
  399.     if (rc == RTCAN) return RTNORM;
  400.     rc = ads_getint(/*MSG6*/"\nNumber of rotations: ", &nt);
  401.     if (rc == RTCAN) return RTNORM;
  402.     rc = ads_getreal(/*MSG7*/"\nGrowth per rotation: ", &cf);
  403.     if (rc == RTCAN) return RTNORM;
  404.     lp = 30;
  405.     ads_initget(0, NULL);
  406.     rc = ads_getint(/*MSG8*/"\nPoints per rotation <30>: ", &lp);
  407.     if (rc == RTCAN) return RTNORM;
  408.  
  409.     rc = grspiral(nt, bp, cf, lp);
  410.  
  411.     return rc;
  412. }
  413.  
  414.